package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.qo.CustomerReportQueryObject;
import cn.wolfcode.crm.service.ICustomerReportService;
import cn.wolfcode.crm.util.MessageUtil;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/customerReport")
public class CustomerReportController {
    @Autowired
    private ICustomerReportService customerReportService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") CustomerReportQueryObject qo) {
        PageInfo<HashMap> pageInfo = customerReportService.query(qo);
        model.addAttribute("result", pageInfo);
        return "/customerReport/list";
    }

    /**
     * 柱状图
     *
     * @return
     */
    @RequestMapping("/listBar")
    public String listBar(Model model, @ModelAttribute("qo") CustomerReportQueryObject qo) {
//        qo.setPageSize(0);
//        PageInfo<HashMap> pageInfo = customerReportService.query(qo);
        List<HashMap> list = customerReportService.selectAll(qo);

        ArrayList<String> xList = new ArrayList<>();
        ArrayList<Integer> yList = new ArrayList<>();
        for (HashMap map : list) {
            xList.add(map.get("groupType").toString());
            yList.add(Integer.valueOf(map.get("number").toString()));
        }
        model.addAttribute("xList", JSON.toJSONString(xList));
        model.addAttribute("yList", JSON.toJSONString(yList));
        model.addAttribute("groupTypeName", MessageUtil.changMsg(qo));
        return "customerReport/listBar";
    }

    /**
     * 柱状图
     *
     * @return
     */
    @RequestMapping("/listPie")
    public String listPie(Model model, @ModelAttribute("qo") CustomerReportQueryObject qo) {
//        qo.setPageSize(0);
//        PageInfo<HashMap> pageInfo = customerReportService.query(qo);
        List<HashMap> list = customerReportService.selectAll(qo);

        ArrayList<String> legend = new ArrayList<>();
        ArrayList<Map> series = new ArrayList<>();
        for (HashMap map : list) {
            legend.add(map.get("groupType").toString());
            HashMap<String, Object> tem = new HashMap<>();
            tem.put("value", map.get("number"));
            tem.put("name", map.get("groupType"));
            series.add(tem);
        }
        model.addAttribute("legend", JSON.toJSONString(legend));
        model.addAttribute("series", JSON.toJSONString(series));
        model.addAttribute("groupTypeName", MessageUtil.changMsg(qo));

        return "customerReport/listPie";
    }
}
